using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XQ.RequestOfficeSupplies.BLL.Common;
using XQ.RequestOfficeSupplies.DAL.DAL.Business;
using XQ.RequestOfficeSupplies.DAL.Model.Business;
using XQ.RequestOfficeSupplies.DAL.Model;
using XQ.RequestOfficeSupplies.DAL.Models;
using XQ.RequestOfficeSupplies.DAL;
using Org.BouncyCastle.Crypto;

namespace XQ.RequestOfficeSupplies.BLL.Business
{
    public class ApplicationFormBLL
    {
        ApplicationFormDal applicationFormDal = new ApplicationFormDal();
        ApplicationFormOfficeSuppliesDal applicationFormOfficeSuppliesDal = new ApplicationFormOfficeSuppliesDal();

        public List<ApplicationFormReturnModel> GetList(ApplicationFormSearchModel ssm, Pagination page, TokenModel token)
        {
            return applicationFormDal.GetList(ssm, page, token);
        }

        public Guid SaveApplicationForm(ApplicationFormSaveModel applicationForm, TokenModel token)
        {
            Guid ApplicationFormID = applicationForm.id;
            bool isSuccess = false;
            DateTime curTime = DateTime.Now;
            if (applicationForm != null && applicationForm.id != Guid.Empty)
            {
                //修改
                applicationForm.updateTime = curTime;
                isSuccess = applicationFormDal.UpdateModel(applicationForm) > 0;

            }
            else
            {
                //新增
                applicationForm.id = Guid.NewGuid();
                ApplicationForm applicationFormInfo = applicationFormDal.AddModel(applicationForm);
                ApplicationFormID = applicationFormInfo.id;

            }
            //保存关联物品
            if (applicationForm.OfficeSupplies != null && applicationForm.OfficeSupplies.Count > 0)
            {
                //删除旧关联
                applicationFormOfficeSuppliesDal.DeleteModel(os => os.applicationFormID == applicationForm.id);
                //保存新关联
                foreach (ApplicationForm_OfficeSupplies afos in applicationForm.OfficeSupplies)
                {
                    afos.applicationFormID = applicationForm.id;
                    afos.addTime = curTime;
                    afos.addUserID = token.UID;
                }
                applicationFormOfficeSuppliesDal.AddModels(applicationForm.OfficeSupplies);
            }


            return ApplicationFormID;
        }

        public ApplicationFormReturnModel GetApplicationFormByID(Guid id)
        {
            ApplicationFormReturnModel applicationFormModel = new ApplicationFormReturnModel();
            ApplicationForm applicationForm = applicationFormDal.FindModel(c => c.id == id);
            applicationFormModel = ClassHelper.ParentCopyToChild<ApplicationForm, ApplicationFormReturnModel>(applicationForm);

            return applicationFormModel;
        }


        public int DeleteByIds(List<Guid> ids)
        {
            //先删除关联物品
            applicationFormOfficeSuppliesDal.DeleteModel(os => ids.Contains((Guid)os.applicationFormID));
            //删除申请单
            return applicationFormDal.DeleteModel(c => ids.Contains(c.id));
        }
    }
}
